package cn.easybuy.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.Easybuy_UserDao;
import cn.easybuy.entity.Easybuy_User;

public class Easybuy_UserDaoImpl extends BaseDao implements Easybuy_UserDao {
	// 用户注册功能
	public int adduser(Easybuy_User easybuy_User) {
		int a = 0;
		String sql = "insert  into `easybuy_user`(`loginName`,`userName`,`password`,`sex`,`identityCode`,`email`,`mobile`,`type`)  values(?,?,?,?,?,?,?,?)";
		Object[] params = { easybuy_User.getLoginName(),
				easybuy_User.getUserName(), easybuy_User.getPassword(),
				easybuy_User.getSex(), easybuy_User.getIdentityCode(),
				easybuy_User.getEmail(), easybuy_User.getMobile(),
				easybuy_User.getType() };
		a = this.executeUpdate(sql, params);

		return a;
	}

	// 遍历用户
	public List<Easybuy_User> getAllnews() {
		List<Easybuy_User> list = new ArrayList<Easybuy_User>();
		String sql = "select * from `easybuy_user` ";
		ResultSet rs = null;
		rs = this.exxcuteQuery(sql);
		Easybuy_User easybuy_User = null;
		try {
			while (rs.next()) {
				easybuy_User = new Easybuy_User();
				easybuy_User.setId(rs.getInt("id"));
				easybuy_User.setLoginName(rs.getString("loginName"));
				easybuy_User.setUserName(rs.getString("userName"));
				easybuy_User.setPassword(rs.getString("password"));
				easybuy_User.setSex(rs.getInt("sex"));
				easybuy_User.setIdentityCode(rs.getString("identityCode"));
				easybuy_User.setEmail(rs.getString("email"));
				easybuy_User.setMobile(rs.getString("mobile"));
				easybuy_User.setType(rs.getInt("type"));
				list.add(easybuy_User);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public Easybuy_User getAllLoginName(String loginName) {
		ResultSet rs = null;
		Easybuy_User user = null;
		String sql = "SELECT * FROM `easybuy_user`where loginName = ?";
		try {
			rs = this.exxcuteQuery(sql, loginName);

			while (rs.next()) {
				user = new Easybuy_User();
				user.setLoginName(loginName);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			closeAll(connection, pstmt, rs);
		}
		return user;
	}

	@Override
	public Easybuy_User getUserPassword(String loginName, String password) {
		ResultSet rs = null;
		Easybuy_User user = null;
		String sql = "SELECT * FROM `easybuy_user`where loginName = ? and password=?";
		Object[] objects = { loginName, password };
		try {
			rs = this.exxcuteQuery(sql,objects );

			while (rs.next()) {
				user = new Easybuy_User();
				user.setLoginName(loginName);
				user.setPassword(password);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			closeAll(connection, pstmt, rs);
		}
		return user;
	}

}
